package com.zhang.authoritycenter.common.config;

import com.alibaba.fastjson.JSON;
import com.zhang.infrastructure.model.KingHoodResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

   @Override
   public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {

      log.error("无法访问：{}",accessDeniedException.getMessage());
      response.setContentType("application/json;charset=utf-8");
      KingHoodResponseVO<Object> kingHoodResponseVO = KingHoodResponseVO.fail(HttpServletResponse.SC_FORBIDDEN,"请检查token信息" );
      response.getWriter().println(JSON.toJSONString(kingHoodResponseVO));
      response.getWriter().flush();
   }
}
